// pages/home/category/index.js
// 接口
import { fetchShopSptype, fetchScoreshopSptype } from '~/request/api';
const image = 'https://tdesign.gtimg.com/miniprogram/images/example2.png';
const items = new Array(12).fill({ label: '标题文字', image }, 0, 12);

Page({
  data: {
    sideBarIndex: 3,
    scrollTop: 0,
    categories: [
      {
        label: '选项一',
        title: '标题一',
        items,
      },
      {
        label: '选项二',
        title: '标题二',
        items: items.slice(0, 10),
      },
      {
        label: '选项三',
        title: '标题三',
        items: items.slice(0, 6),
      },
      {
        label: '选项四',
        title: '标题四',
        items: items.slice(0, 8),
      },
      {
        label: '选项五',
        title: '标题五',
        items: [],
      },
    ],
    list: []
  },

  // 获取类目
  async shopSptype(tabid) {
    try {
      const res = await fetchShopSptype();
      this.setData({ list: res.children });
    } catch (error) {
      console.log(error);
    }
  },

  // 获取积分类目
  async scoreShopSptype(tabid) {
    try {
      const res = await fetchScoreshopSptype();
      this.setData({ list: res.children });
    } catch (error) {
      console.log(error);
    }
  },

  onSideBarChange(e) {
    const { value } = e.detail;
    this.setData({ sideBarIndex: value });
  },

  handleItem(e) {
    // const { title } = e.currentTarget.dataset;
    const { id } = e.currentTarget
    wx.navigateTo({
      url: '/pages/goods/result/index?type_id=' + id,
    });
  },

  onLoad(options) {
    this.setData({ sideBarIndex: +options?.tab });
    // 商品类目
    if (options?.type !== 'integral') {
      this.shopSptype(options?.tab);
    } else {
      // 积分商品类目
      this.scoreShopSptype(options?.tab);
    }
  },
});
